//
//  UIButtonExample.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2022/6/21.
//

import UIKit
import SnapKit

class UIButtonExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "setTitle", description: "默认值为 nil", action: { [weak self] in self?.setTitle() }),
    .init(title: "setTitleColor", description: "默认值 white", action: { [weak self] in  self?.setTitleColor() }),
    .init(title: "setFontSize", description: "默认值 17.0", action: { [weak self] in self?.setFontSize() }),
  ]
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    button.layer.borderWidth = 1.0
    button.layer.borderColor = UIColor.gray.cgColor
    
    view.addSubview(button)
    button.snp.makeConstraints { make in
      make.center.equalToSuperview()
    }
  }
  
  private var button = UIButton()
  
  private func setTitle() {
    // 显示在按钮上的当前标题。默认值为 nil。
    button.setTitle(button.currentTitle == nil ? "这是一个按钮" : nil, for: .normal)
  }
  
  private func setTitleColor() {
    // 用于显示标题的颜色。默认值为 white。
    let color = button.currentTitleColor == UIColor.white ? UIColor.black : UIColor.white
    button.setTitleColor(color, for: .normal)
  }
  
  private func setFontSize() {
    guard let font = button.titleLabel?.font else { return }
    print("font:", font)
    // 默认值 17.0
    if font.pointSize == 17.0 {
      button.titleLabel?.font = .systemFont(ofSize: 14)
    } else {
      button.titleLabel?.font = .systemFont(ofSize: 17)
    }
  }
}
